// pages/confirmConsultation/index.js
import {
  ApiPatientCardList,
  ApiConsultationOrder
} from '../../api/index'
const app = getApp()
let {
  baseUrl,
  commonUpdate,
  commonUpload
} = app.globalData
Page({

  /**
   * 页面的初始数据
   */
  data: {

    isLoading: false,
    autosize: {
      maxHeight: 120,
      minHeight: 80
    },
    baseUrl,
    commonUpdate,
    commonUpload,
    patient_pic: [],
    targetArr: ['健康咨询', '在线复诊', '在线续方'],
    opt: {
      patient_pic: [],
      doctor_id: '',
      patient_id: '',
      department_id: '',
      order_time: '',
      patient_status: '',
      target: '健康咨询'
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  /*   data={"value1":"15:41","value2":"15:41","value3":"12"}&doctorInfo={"id":1,"name":"内科医生","department":"消化内科","icon":"files/admin/20241128/20241128155745_01.png","level":"主治医师","introduce":"专业擅长","order_count":3,"score":0,"price":"0.00"} */
  onLoad(options) {
    this.setData({
      doctorInfo: options.doctorInfo ? JSON.parse(options.doctorInfo) : '',
      dataSelect: options.dataSelect ? JSON.parse(options.dataSelect) : '',
      data: options.data ? JSON.parse(options.data) : '',
      'opt.order_type': wx.getStorageSync('order_type'),
      consultation_type: options.consultation_type
    })


    //获取就诊人列表(防止选中的值进行改变)
    this.getPatientList()
  },

  /* 添加就诊人 */
  createPatientCard() {
    wx.navigateTo({
      url: '/pages/patientCard/index',
    })
  },

  onPreview(event) {
    let val = event.currentTarget.dataset.v
    wx.previewImage({
      urls: [val],
      current: val,
    })
  },
  handleDelete(event) {
    console.log(event, 'eventeventevent')
    let val = event.currentTarget.dataset.v
    const index = this.data.opt.patient_pic.findIndex(item => {
      return item == val
    })
    console.log(index, this.data.opt.patient_pic)
    this.data.opt.patient_pic.splice(index, 1)
    this.setData({
      'opt.patient_pic': this.data.opt.patient_pic,
    })
  },

  getPatientList() {
    let that = this
    wx.showLoading({
      title: '加载中'
    })
    ApiPatientCardList().then(res => {
      res.data.forEach((item, index) => {
        if (item.use == '1') {
          that.setData({
            defaultPatient: item,
            active: index
          })
          wx.hideLoading()
        }
      })
      this.setData({
        patientCardList: res.data
      })
    })
    wx.hideLoading()
  },
  //切换就诊人
  clickPatientCardList(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      active: index,
      defaultPatient: this.data.patientCardList[index]
    })
  },

  //问诊目的
  getTarget() {
    this.setData({
      showpopup: true
    })
  },

  confirmBtn(e) {
    this.setData({
      'opt.target': e.detail.value,
      showpopup: false
    })
  },
  cancelBtn() {
    this.setData({
      showpopup: false
    })
  },

  rules(val, message) {
    if (val == "") {
      wx.showToast({
        title: `${message}不能为空`,
        icon: 'none'
      });
      return false;
    } else {
      return true
    }
  },
  //提交问诊的界面 
  submitConsultation(e, newOrder) {
    if (this.rules(this.data.opt.target, '问诊目的') && this.rules(this.data.opt.patient_status, '病情描述') && this.rules(this.data.opt.patient_pic, '病情描述图片')) {
      let that = this
      this.setData({
        isLoading: true
      })
      this.data.opt.newOrder = newOrder
      this.data.opt.doctor_id = this.data.doctorInfo.id
      this.data.opt.patient_id = this.data.defaultPatient.id
      this.data.opt.department_id = this.data.doctorInfo.department_id
      this.data.opt.order_time = this.data.dataSelect.date + " " + this.data.data.time
      this.data.opt.patient_pic = this.data.opt.patient_pic.toString()
      this.data.opt.order_type = wx.getStorageSync('order_type')
      this.data.opt.consultation_type = this.data.consultation_type
      if (this.data.opt.order_type == '护理咨询' || this.data.opt.order_type == '药师咨询') {
        delete this.data.opt.target
        delete this.data.opt.order_time
      } else {
        delete this.data.opt.consultation_type
      }
      ApiConsultationOrder(this.data.opt).then(res => {
        if (res.code == 200) {
          if (res.data.type == "notPay") {
            wx.showModal({
              title: '请勿重复下单',
              cancelText: '支付上笔',
              confirmText: '继续支付',
              confirmColor: '#41CFB7',
              success(e) {
                if (e.confirm) {
                  that.submitConsultation('', '1')
                  //支付上比
                } else if (e.cancel) {
                  wx.navigateTo({
                    url: `/pages/submitConsultation/index?order=${JSON.stringify(res.data.order)}`,
                  })
                  /*     console.log('用户点击取消') */
                }
              }
            })
          } else {
            //继续支付订单
            wx.navigateTo({
              url: `/pages/submitConsultation/index?order=${JSON.stringify(res.data.order)}`,
            })
          }
        }
      }).finally(() => {
        this.setData({
          isLoading: false
        })
      })


    }





  },


  onChangeArea(e) {
    this.setData({
      'opt.patient_status': e.detail
    })
  },

  onclickIncrease() {
    this.setData({
      show: true
    })
  },

  getUploadImage(e) {
    let eVal = e.detail.value
    this.data.opt.patient_pic.push(eVal)
    this.setData({
      'opt.patient_pic': this.data.opt.patient_pic
    })
  },
  //
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})